
function requestData() {
    var userId;
    $.ajax({
        url:"/shoppingcar/showcar",
        dataType:"json",
        type:"get",
        success:function (data) {
            // console.log(data)
            $.each(data,function (i,result) {
                userId = result.userId;
            })
            showData(data,userId)
                // console.log(userId)
        }
    })

}


//购物车删除游戏
function deletecar(userId,gameId) {
    console.log(userId,gameId)
    $.ajax({
        url: "/shoppingcar/deletecar",
        data: {
            userId: userId ,
            gameId:gameId
        },
        type:"post",
        success:function (data) {
            console.log(data);
            if (data === "success"){
                alert("删除成功")
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            alert(XMLHttpRequest.readyState + XMLHttpRequest.status + XMLHttpRequest.responseText);
        }
    })
}

//删除所有游戏
function deleteall(userId) {
    $.ajax({
        url: "/shoppingcar/deleteall",
        data: {
            userId: userId
        },
        type:"post",
        success:function (data) {
            console.log(data);
            if (data === "success"){
                alert("删除成功")
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            alert(XMLHttpRequest.readyState + XMLHttpRequest.status + XMLHttpRequest.responseText);
        }
    })

}

function payall(userId) {
    $.ajax({
        url: "/shoppingcar/deleteall",
        data: {
            userId: userId
        },
        type:"post",
        success:function (data) {
            console.log(data);
            if (data === "success"){
                alert("购买成功")
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            alert(XMLHttpRequest.readyState + XMLHttpRequest.status + XMLHttpRequest.responseText);
        }
    })

}
function totalPrice(tot) {
    var sum = 0;//初始化总价
    for(var i=0;i<tot.length;i++){
        sum += tot[i] //单个总价相加
    }
    // console.log(sum)
    $("#total").text("￥"+ sum)
}



function showData(jsonObj,userId){
    var total_price = [];
    delgame(userId);
    pay(userId)
    var html =''
    for(var i = 0; i < jsonObj.length; i ++) {
        total_price[i] = jsonObj[i].gamePrice
        html = html + "<div class='message'><div class='message-left'>"
        html = html +"<img src='" + jsonObj[i].gamePicture + "' ></img>"
        html = html + "<p>"+jsonObj[i].gameName+"</p></div>"
        html = html + "<div class='message-right'>"
        html = html + "<div class='right-top'><p>￥"+ jsonObj[i].gamePrice +"</p></div>"
        html = html + "<div class='right-bottom' ><a href='' onclick='deletecar(" + jsonObj[i].userId + "," + jsonObj[i].gameId + ")'>移除</a></div></div>"
        html = html +"</div>"
    }
    $('.message1').html(html)
    totalPrice(total_price)

}



function delgame(obj) {
    console.log(obj)
    var html =''
    html = html +"<a href='' onclick='deleteall("+obj +")'>移除所有物品</a>"
    $('.continue-right').html(html)
}

function pay(obj1) {
    var html =''
    html = html +"<button onclick='payall("+obj1 +")'>结&nbsp;&nbsp;算</button>"
    $('.pay-bottom').html(html)
}


requestData();


